package com.quick.develop.form.process;

import com.quick.develop.form.utils.FormUtils;
import com.quick.develop.framework.common.annotation.*;
import liquibase.pro.packaged.S;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
public class BusinessScanProcess implements BeanPostProcessor {

    public static final Map<String, Method> definitionMethods = new HashMap<>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, @NotNull String beanName) throws BeansException {
        Class<?> aClass = bean.getClass();
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations) {
                Class<? extends Annotation> aClass1 = annotation.annotationType();
                if (aClass1.equals(DetailForm.class)
                        || aClass1.equals(CreateForm.class)
                        || aClass1.equals(UpdateForm.class)
                        || aClass1.equals(DeleteForm.class)
                        || aClass1.equals(QueryTable.class)) {
                    String annotationName = aClass1.getName();
                    Method ifAbsent = definitionMethods.putIfAbsent(FormUtils.parserBusinessFormKey(beanName, annotationName), method);
                    if (Objects.nonNull(ifAbsent)) {
                        log.error("存在重复的注解定义，beanName:{}, annotationName:{}", beanName, annotationName);
                    }
                }
            }
        }
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

}
